package com.easthome.process.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.easthome.common.result.Result;
import com.easthome.model.process.Process;
import com.easthome.model.process.ProcessTemplate;
import com.easthome.process.service.ProcessService;
import com.easthome.process.service.ProcessTemplateService;
import com.easthome.process.service.ProcessTypeService;
import com.easthome.vo.process.ApprovalVo;
import com.easthome.vo.process.ProcessFormVo;
import com.easthome.vo.process.ProcessVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: 朱敦宇
 * @time: 2023/11/16 14:06
 */
@Api(tags = "审批流管理")
@RestController
@RequestMapping("/admin/process")
@CrossOrigin(origins = "*") //跨域
public class ProcessApiController {
    @Autowired
    private ProcessTypeService processTypeService;
    @Autowired
    private ProcessTemplateService processTemplateService;
    @Autowired
    private ProcessService processService;
    @ApiOperation("获取全部审批分类及模板")
    @GetMapping("/findProcessType")
    public Result findProcessType(){
        return processTypeService.findProcessType();
    }

    @ApiOperation("获取模板")
    @GetMapping("/getProcessTemplate/{processTemplateId}")
    public Result getProcessTemplate(@PathVariable Long processTemplateId){
        ProcessTemplate byId = processTemplateService.getById(processTemplateId);
         return Result.ok(byId);
    }

    @ApiOperation("提交审批")
    @PostMapping("/startUp")
    public Result startUp(@RequestBody ProcessFormVo processFormVo){
       return processService.startup(processFormVo);
    }
    @ApiOperation(value = "待处理")
    @GetMapping("/findPending/{page}/{limit}")
    public Result findPending(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,
            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit) {
        Page<Process> pageParam = new Page<>(page, limit);
        IPage<ProcessVo> processVoIPage = processService.findPending(pageParam);
        return Result.ok(processVoIPage);
    }

    @ApiOperation("获取待处理任务详情")
    @GetMapping("/show/{id}")
    public Result show(@PathVariable("id") Long processId){

     Map<String,Object> map = processService.show(processId);
        return Result.ok(map);
    }

    @ApiOperation(value = "审批")
    @PostMapping("approve")
    public Result approve(@RequestBody ApprovalVo approvalVo) {
        processService.approve(approvalVo);
        return Result.ok();
    }
    @ApiOperation(value = "已处理")
    @GetMapping("/findProcessed/{page}/{limit}")
    public Result findProcessed(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,
            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit) {
        Page<Process> pageParam = new Page<>(page, limit);
        IPage<ProcessVo> voIPage = processService.findProcessed(pageParam);
        return Result.ok(voIPage);
    }

    @ApiOperation(value = "已发起")
    @GetMapping("/findStarted/{page}/{limit}")
    public Result findStarted(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,
            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit) {
        Page<ProcessVo> pageParam = new Page<>(page, limit);
         IPage<ProcessVo> processVoIPage =  processService.findStarted(pageParam);
        return Result.ok(processVoIPage);
    }


}
